<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Banner extends Sys_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Banner_model');
        $this->load->library('ajaxMsgModel');
        $this->load->library('form_validation');
    }

    public function BannerList()
    {
        $this->ajaxmsgmodel->Type='1';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $this->load->library('pagination');
        $perPage=10;
        $config['base_url']=base_url('admin/banner/bannerlist');
        $config['per_page']=$perPage;
        $config['total_rows']=$this->Banner_model->Get_AllRow(); //$this->db->count_all_results('product');
        $config['uri_segment']=4;
        $this->pagination->initialize($config);
        $data['links']=$this->pagination->create_links();
        $offset=$this->uri->segment(4);
        $this->db->limit($perPage,$offset);
        $data['bannerList']=$this->Banner_model->Get_BannerList_Page();
        $this->load->view('admin/banner/bannerlist.html',$data);
    }

    public function AddBanner()
    {
        $this->ajaxmsgmodel->Type='2';
        $this->Ajax_Check($this->ajaxmsgmodel);
        $this->load->view('admin/banner/addbanner.html');
    }

    public function Add_Banner()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $this->load->library('ajaxMsgModel');
        $bannerModel=new $this->Banner_model();
        $bannerModel->imgurl=$this->input->post('imgurl');
        $bannerModel->isuse=$this->input->post('isuse');
        $bannerModel->weight=$this->input->post('weight');
        $res= $bannerModel->Add_Banner($bannerModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=site_url('admin/banner/bannerlist');
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function Set_Banner_Weight()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $this->load->library('ajaxMsgModel');
        $bannerModel=new $this->Banner_model();
        $bannerModel->bid=$this->uri->segment(4);
        $bannerModel->weight=$this->input->post('weight');
        $backurl=$this->input->get('returnurl');
        $res=$bannerModel->Set_Banner_Weight($bannerModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function EditBanner()
    {
        $this->ajaxmsgmodel->Type='2';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $bid=$this->uri->segment(4);
        $banner_Model=$this->Banner_model->Get_Banner_ById($bid);
        $data['bannerModel']=$banner_Model;
        $data['backurl']=$this->input->get('returnurl');
        $this->load->view('admin/banner/editbanner.html',$data);
    }

    public function Edit_Banner()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $this->load->library('ajaxMsgModel');
        $bannerModel=new $this->Banner_model();
        $bannerModel->bid=$this->input->post('bid');
        $bannerModel->weight=$this->input->post('weight');
        $bannerModel->imgurl=$this->input->post('imgurl');
        $bannerModel->isuse=$this->input->post('isuse');
        $backurl=$this->input->get('returnurl');
        $res=$bannerModel->Edit_Banner($bannerModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function Batchdel_Banner()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $bids=$this->input->post('items');
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $bannerModel=new $this->Banner_model();
        $bidsArray=explode('|',trim($bids,'|'));
        if(count($bidsArray)>0)
        {
            $res=$bannerModel->BatchDel_Banner($bidsArray);
            if($res>0)
            {
                $this->ajaxmsgmodel->Msg='操作成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=$backurl;
            }
            else
            {
                $this->ajaxmsgmodel->Msg='操作失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }else
        {
            $this->ajaxmsgmodel->Msg='请选择删除的记录';
            $this->ajaxmsgmodel->Statu='err';
        }

        echo(json_encode($this->ajaxmsgmodel));
    }

    public function Del_Banner()
    {
        $this->ajaxmsgmodel->Type='4';
        $this->Ajax_Check($this->ajaxmsgmodel);

        $this->load->library('ajaxMsgModel');
        $bannerModel=new $this->Banner_model();
        $bannerModel->bid=$this->uri->segment(4);
        $backurl=$this->input->get('returnurl');
        $res=$bannerModel->Del_Banner($bannerModel);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
}
?>
